package com.android.mall.utils;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

public class ThreadPool {

	private static ThreadPool threadPool = null;
	private ExecutorService exec;
	
	public ThreadPool(){
		
		exec = Executors.newCachedThreadPool(new MyThreadFactory());
	}
	
	public static ThreadPool getInstance(){
		
		if(threadPool == null){
			threadPool = new ThreadPool();
		}
		
		return threadPool;
	}
	
	public void Add(Runnable run){
		if ( (exec != null)&&(run != null) )
			exec.execute(run);
		
		/*
		Runnable run = new Runnable() {  
            public void run() {  
                long time = (long) (Math.random() * 1000);  
                System.out.println("Sleeping " + time + "ms" + "/" + Thread.currentThread().getName());  

                try {  
                    Thread.sleep(time);  
                } catch (InterruptedException e) {  
                }  
            }  
        };  
        */
	}
	
	public void Shutdown(){
		if(exec != null)
			exec.shutdown();
	}
	
}

class MyThreadFactory implements ThreadFactory{

	public Thread newThread(Runnable r) {
		// TODO Auto-generated method stub
		
		Thread t = new Thread(r);
		return t;
	}


	
}